/*
Copyright 2012 Cuong Pham

This file is part of eTrack project.

eTrack is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

eTrack is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with eTrack. If not, see http://www.gnu.org/licenses/.
*/

/***********************************************************************************************
 *    Project Name: eTrack
 *    Project website: http://code.google.com/p/etrack/
 * *********************************************************************************************/

#include <XBee.h>

#define RESET 100 
#define BRCAST 101 
#define RSSI 102 

XBee xbee = XBee();
uint8_t payload[] = { 0, 0, 0 };

// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx16 = Tx16Request(0x0000, payload, sizeof(payload));

// 64-bit addressing: This is the SH + SL address of remote XBee
XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000FFFF);
// unless you have MY on the receiving radio set to FFFF, this will be received as a RX16 packet
Tx64Request tx64 = Tx64Request(addr64, payload, sizeof(payload));

TxStatusResponse txStatus = TxStatusResponse();

int statusLed = 13;
uint8_t serial = 0;

void flashLed(int pin, int times, int wait) {
    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);
   
      if (i + 1 < times) {
        delay(wait);
      }
    }
}

void setup() {
  pinMode(statusLed, OUTPUT);  
  xbee.begin(9600);
}

void loop() {
  sendMsg64(BRCAST);
}

void sendMsg16(int type) {
  payload[0]=type;
  payload[1]=0;
  payload[2]=0;
  
  xbee.send(tx16);
  
  // flash TX indicator
  flashLed(statusLed, 1, 100);

  // after sending a tx request, we expect a status response
  // wait up to 5 seconds for the status response
  if (xbee.readPacket(1000)) {
    // got a response!

    // should be a znet tx status                
    if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
            
      // get the delivery status, the fifth byte
      if (txStatus.getStatus() == SUCCESS) {
        // success.  time to celebrate
      } else {
          // the remote XBee did not receive our packet. is it powered on?
          sendMsg16(type);
        }
      }      
    } else if (xbee.getResponse().isError()) {
          sendMsg16(type);
    } else {
        // local XBee did not provide a timely TX Status Response.  Radio is not configured properly or connected
          sendMsg16(type);        
    }    
    delay(1000);  
}

void sendMsg64(int type) {
  payload[0]=type;
  payload[1]=serial++;  
  payload[2]=0;  
  
  xbee.send(tx64);
  
  // flash TX indicator
  flashLed(statusLed, 2, 100);

  delay(1000);  
}



